import _ from 'lodash'

export default {
    provide() {  // 这里定义provide传递参数
        return {
            pageData: this.pageData || [],  // 表格数据
            pagination: this.pagination  // 分页属性
        }
    },
    data() {
        return {
            pageData: [],  //  当前页面展示数据 
            allData: [],  // 假设这是后台传来的数据来源
            totalPage: [],  //  所有页数据
            pageNum: 0,  // 共几页
            dataShow: '', // 当前页显示数据
            pagination: {
                currentPage: 1,
                total: 0,
                pageSize: 10
            },
            staticPagination: false
        }
    },
    methods: {
        onGotPageData({content, totalElements, size, number}) {  //onGotPageData()函数，在页面返回数据后调用，给分页组件设置后台返回的值
            console.log('onGotPageData');
            console.log({content, totalElements, size, number});
            if (content.length > size) {
                this.staticPagination = true;
                this.allData = content;
                this.pageData = this.countStaticDataPage({contentData: content, dataSize: size, currentNumber: number});
            } else {
                this.pagination.currentPage = number;
                this.pagination.pageSize = size;
            }
            this.pagination.total = totalElements;
        },
        onPageChanged({currentPage, pageSize}) {   //  onPageChanged()函数，页码改变触发，然后调用父组件的getData()函数
            console.log('mixins');
            console.log({currentPage, pageSize});
            console.log(this.pagination.currentPage);
            console.log(this.pagination.pageSize);
            if (this.staticPagination) {
                if (this.pagination.pageSize === pageSize) {
                    this.pageData = this.totalPage[currentPage - 1];
                }
                if (this.pagination.currentPage === currentPage) {
                    this.pageData = this.countStaticDataPage({contentData: this.allData, dataSize: pageSize, currentNumber: 1});
                }
            } else {
                this.pagination.currentPage = currentPage;
                if (this.getData) {
                    this.getData();
                }
            }
            this.pagination.pageSize = pageSize;
        },
        addPageInfo(params = {}) {  //  addPageInfo()函数，把请求后台的参数传递进来，然后加上分页参数，包装成一个对象返回
            Object.assign(params, {
                pageSize: this.pagination.pageSize,
                pageNumber: this.pagination.currentPage - 1
            });
            return params;
        },
        countStaticDataPage({contentData, dataSize, currentNumber}) {  //  countStaticDataPage() 前端假分页
            let dataShow = [];
            // 根据后台数据的条数和每页显示数量算出一共几页,得0时设为1 
            const pageNum = Math.ceil(contentData.length / dataSize) || 1;
            // 每一页都是一个数组 形如 [['第一页的数据'],['第二页的数据'],['第三页数据']]
            // 根据每页显示数量 将后台的数据分割到 每一页,假设pageSize为5， 则第一页是1-5(对slice操作来说是0-5)条，第二页是6-10(对slice操作来说是5-10)条...
            for (let i = 0; i < pageNum; i++) {
                this.totalPage[i] = contentData.slice(dataSize * i, dataSize * (i + 1));
            }
            // 当前显示的内容
            dataShow = this.totalPage[currentNumber - 1];
            return dataShow;
        }
    }
}